import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
import { InnerMenu } from 'src/app/models/inner-menu';
import { PermitItem } from 'src/app/models/permitItem';
import { PageService, WorkflowService } from 'src/app/services';
import { MenuService } from 'src/app/services/menu.service';
import { OrganizationService } from 'src/app/services/organization.service';


@Component({
  selector: 'zepride-menu-panel',
  templateUrl: './menu-panel.component.html',
  styleUrls: ['./menu-panel.component.less']
})
export class MenuPanelComponent implements OnInit {
  private _premits: PermitItem[] = [];
  @Input() set permits(value) {
    this._premits = value;

    this.s_menus || (this.s_menus = []);
    this.t_menus || (this.t_menus = []);
    // this.selectPermits();
  }
  menus = [];
  s_menus = [];
  t_menus = [];

  curMenu;
  sCurMenu;
  tCurMenu;

  sMenuShow: boolean;

  adminMenuItemSelectd = false;

  @Output() pageDataChanged: EventEmitter<{ pageData: any; menu: any, smenu: any }> = new EventEmitter();
  constructor(
    private menuSvr: MenuService,
    private orgSvr: OrganizationService,
    private pSvr: PageService,
    private wSvr: WorkflowService,
  ) { }

  async ngOnInit() {
    await this.getMenus();
  }

  private async getMenus() {
    try {
      const r = await this.menuSvr.queryMenus({
        orgId: this.orgSvr.orgid
      });

      r.list.map(x => {
        if (!x.parentId) {
          x.parentId = 'root';
        }
      });

      this.menus = r.list.filter(x => x.parentId == 'root');

      for (const item of this.menus) {
        const c = r.list.filter(x => x.parentId == item.id);
        if (c && c.length) {
          item.children || (item.children = []);
          item.children = item.children.concat(c);
        }
      }
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  expanl(item, level = 0) {
    this.adminMenuItemSelectd = false;

    this.sCurMenu = null;
    this.tCurMenu = null;

    if (level == 0) {
      this.s_menus = item.children;
      this.curMenu = item;
    }
    else if (level == 1) {
      this.t_menus = item.children;
      this.sCurMenu = item;
    }
    else if (level == 2) {
      this.tCurMenu = item;
    }

    if (item.children && item.children.length > 0) {
      return;
    }

    if (item.workflowId) {
      this.getWorkflowById(item.workflowId, item);
    }
    else if (item.pageId) {
      this.getPageById(item.pageId, item);
    }
  }

  parentMenuMouseOver(item) {
    if (item.children && item.children.length > 0) {
      this.expanl(item);
      this.sMenuShow = true;
    }
  }

  childrenMouseLeave() {
    this.sMenuShow = false;
  }

  async getPageById(pageId: string, menu) {
    try {
      const r = await this.pSvr.getPageById(pageId);
      this.pageDataChanged.emit({
        pageData: r,
        menu: this.curMenu,
        smenu: this.sCurMenu
      });
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  async getWorkflowById(id: string, menu) {
    try {
      const r = await this.wSvr.getWorkflowById(id);
      r.type = 'workflow';
      this.pageDataChanged.emit({
        pageData: r,
        menu: this.curMenu,
        smenu: this.sCurMenu
      });
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

}
